//
//  MainViewController.m
//  RoadVehicles
//
//  Created by appleLJ on 2018/2/26.
//  Copyright © 2018年 appleLJ. All rights reserved.
//

#import "MainViewController.h"
#import "HomeViewController.h"
#import "DiscoverViewController.h"
#import "LendViewController.h"
//#import "BorrowViewController.h"
#import "MineViewController.h"
#import "IWTabBar.h"

@interface MainViewController ()<IWTabBarDelegate>

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化一个自定义的tabbar
    IWTabBar *tabbar = [[IWTabBar alloc] init];
    tabbar.delegate = self;
    
    //因为是只读属性,所以通过kvc的方式去赋值
    [self setValue:tabbar forKeyPath:@"tabBar"];
    
    HomeViewController *homeVc = [[HomeViewController alloc] init];
    LendViewController *lendVc = [[LendViewController alloc] init];
    DiscoverViewController *discoverVc = [[DiscoverViewController alloc] init];
    MineViewController *mineVc = [[MineViewController alloc] init];
    
    [self addchildViewVc:homeVc withImagName:@"home_unselect" withSelectedImageName:@"home_select" andTitle:@"首页"];
    [self addchildViewVc:lendVc withImagName:@"lend_unselect" withSelectedImageName:@"lend_select" andTitle:@"出借"];
    [self addchildViewVc:discoverVc withImagName:@"find_unselect" withSelectedImageName:@"find_select" andTitle:@"发现"];
    [self addchildViewVc:mineVc withImagName:@"mine_unselect" withSelectedImageName:@"mine_select" andTitle:@"我的"];
        
}

//添加子控制器方法
- (void)addchildViewVc:(UIViewController *)vc withImagName:(NSString *)imgName withSelectedImageName:(NSString *)selectedImgName andTitle:(NSString *)titile{
    //设置标题
    vc.title = titile;
    //vc.navigationController.title = titile;
    //设置选中状态item背景颜色
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:getColor(mainColor),NSFontAttributeName:DEF_FontSize_12} forState:UIControlStateSelected];
    
    UIImage * unSelectImg = [UIImage imageNamed:imgName];
    unSelectImg = [unSelectImg  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage * selectImge = [UIImage imageNamed:selectedImgName];
    selectImge = [selectImge imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //设置图片
    vc.tabBarItem.image = unSelectImg;
    //设置选中图片
    vc.tabBarItem.selectedImage = selectImge;
    
    //添加子控制器
    UINavigationController *navVc = [[UINavigationController alloc] initWithRootViewController:vc];
    [[UINavigationBar appearance] setTranslucent:NO];
    [self addChildViewController:navVc];
}

#pragma mark -- IWTabBarDelegate
- (void)tabBar:(IWTabBar *)tabBar didSelectPlusButton:(UIButton *)button{
    NSLog(@"aaaa");
}

@end
